package com.cn.web.controller.PartsManagement;

import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.lbj.ParameterClassificationTable;
import com.cn.bean.pojo.lbj.ParameterItemTable;
import com.cn.config.config.Result;
import com.cn.service.server.PartsManagement.ParameterClassificationService;
import com.cn.service.server.PartsManagement.ParameterItemTableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@Slf4j
@Transactional
public class ParameterItemTableController {
    @Autowired
    private ParameterItemTableService parameterItemTableService;
    @Autowired(required = false)
    private ParameterClassificationService parameterClassificationService;
    @RequestMapping("/select/ParameterItemTable")
    @ResponseBody
    public List<ParameterItemTable> selectParameterItemTable(@RequestBody ParameterItemTable parameterItemTable) {

        List<ParameterItemTable> businessTypeTables = parameterItemTableService.selectParameterItemTable(parameterItemTable);
        return businessTypeTables;
    }
    @RequestMapping("/select/ParameterItemTable/Aside/All")
    @ResponseBody
    public List<ParameterItemTable> selectParameterItemTableAsideAll(@RequestBody JSONObject jsonObject) {
        Integer parentID = jsonObject.getInteger("parentID");
        List<ParameterItemTable> businessTypeTables = parameterItemTableService.selectAll();

        List<ParameterClassificationTable> componentItemClassifications = parameterClassificationService.selectParameterClassificationAside();
        List<ParameterClassificationTable> result = new ArrayList<>();
        for (ParameterClassificationTable type : componentItemClassifications) {
            System.out.println(type.getId());
            if (type.getId() == parentID) {
                // 找到与指定parentID匹配的根节点，调用递归函数获取其所有子节点
                result.add(type);
                addChildNodes(type, componentItemClassifications, result);
            }
        }
        //result  所有分类
        List<ParameterItemTable> result1 = new ArrayList<>();
        for (ParameterItemTable componentItemClassification : parameterItemTableService.selectAll()) {
            for(ParameterClassificationTable a:result){
                if (componentItemClassification.getClassificationId() == a.getId()) {
                    // 找到与指定parentID匹配的根节点，调用递归函数获取其所有子节点
                    result1.add(componentItemClassification);
                }
            }
        }
        return result1;
    }
    private void addChildNodes(ParameterClassificationTable parent, List<ParameterClassificationTable> allNodes, List<ParameterClassificationTable> result) {
        for (ParameterClassificationTable node : allNodes) {
            if (node.getParentId()== parent.getId()) {
                // 如果当前节点是指定parentID的子节点，将其添加到结果列表中
                result.add(node);
                // 继续递归查找当前节点的子节点
                addChildNodes(node, allNodes, result);
            }
        }
    }
    @RequestMapping("/add/ParameterItemTable")
    @ResponseBody
    public Result addParameterItemTable(@RequestBody JSONObject jsonObject) {
        ParameterItemTable formAsid = jsonObject.getObject("formAsid", ParameterItemTable.class);

        Result result = parameterItemTableService.addParameterItemTable(formAsid.getItemName(),formAsid.getValueRangeType(),formAsid.getSourceMethod(),formAsid.getSourceTable(),formAsid.getSourcePrimaryKeyField(),formAsid.getStandardValueField(),formAsid.getLowerLimitField(),formAsid.getUpperLimitField(),formAsid.getDescription(),formAsid.getEntryTimePoint(),0,formAsid.getClassificationId());
//        return businessTypeService.addBusinessType();
        return result;
    }
    //
    @RequestMapping("/delete/ParameterItemTable")
    @ResponseBody
    public Result deleteParameterItemTable(@RequestBody JSONObject jsonObject) {
        ParameterItemTable asid=new ParameterItemTable();
        asid = jsonObject.getObject("formAsid", ParameterItemTable.class);
//        if (asid.getId()==0||asid.getId()==1){
//            return Result.failure("业务类型字段不可修改");
//        }
        int id = asid.getId();
        return parameterItemTableService.delParameterItemTableStatus(id);
    }

    @RequestMapping("/update/ParameterItemTable")
    @ResponseBody
    public Result updateParameterItemTable(@RequestBody JSONObject jsonObject) {
        ParameterItemTable formAsid = jsonObject.getObject("formAsid", ParameterItemTable.class);
//        if (formAsid.getId()==0||formAsid.getId()==1){
//            return Result.failure("业务类型字段不可删除");
//        }
        Result result = parameterItemTableService.updateParameterItemTable(formAsid.getId(),formAsid.getItemCode(),formAsid.getItemName(),formAsid.getValueRangeType(),formAsid.getSourceMethod(),formAsid.getSourceTable(),formAsid.getSourcePrimaryKeyField(),formAsid.getStandardValueField(),formAsid.getLowerLimitField(),formAsid.getUpperLimitField(),formAsid.getDescription(),formAsid.getEntryTimePoint(),formAsid.getIsdel(),formAsid.getClassificationId());
        return result;
    }
    @RequestMapping("/select/all")
    @ResponseBody
    public List<ParameterItemTable> selectParameterItemTable() {
        return parameterItemTableService.selectAll();
    }
}
